package com.example.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.dao.MenuDao;
import com.example.domain.Menus;
import com.example.result.Result;
import com.example.result.ResultFactory;
import com.example.service.MenuService;
import com.example.tool.DateTool;

/**
 * @author 袁小弘
 * @describe 权限服务实现层
 * @date 2019-12-31
 */
@Service
public class MenuServiceImpl implements MenuService {

    @Autowired
    MenuDao menuDao;

    /**
     * @describe 添加权限信息
     */
    @Override
    public Result addMenus(Menus menu) {
        if (menu != null) {
            // 获取系统当前时间作为记录创建时间
            menu.setRecordTime(DateTool.getTimestamp());
        }
        // 调用JPA保存实体方法
        Menus menu1 = menuDao.save(menu);

        if (menu1 != null) {
            return ResultFactory.buildSuccessResult(menu1);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 更新角色信息
     */
    @Override
    public Result updateMenus(Menus menu) {
        if (menu != null) {
            // 获取系统当前时间作为记录更新时间
            menu.setUpdateTime(DateTool.getTimestamp());
        }
        // 调用JPA保存并刷新实体方法
        Menus menu1 = menuDao.saveAndFlush(menu);

        if (menu1 != null) {
            return ResultFactory.buildSuccessResult(menu1);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 根据编号删除角色信息
     */
    @Override
    public Result deleteMenuById(String menuId) {
        int row = menuDao.deleteByMenuId(Integer.parseInt(menuId));
        if (row > 0) {
            return ResultFactory.buildDefaultSuccessResult();
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 查询所有角色信息
     */
    @Override
    public Result queryAllMenus() {
        List<Menus> list = menuDao.findAll();
        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 根据编号查询角色信息
     */
    @Override
    public Result queryMenuById(String menuId) {
        List<Menus> list = menuDao.findByMenuId(Integer.parseInt(menuId));

        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 通过Excel批量添加用户信息
     */
    @Override
    public Result addMenuByExcel(List<Menus> list) {
        List<Menus> list1 = null;
        if (list != null) {
            for (int i = 0; i < list.size(); i++) {
                list.get(i).setRecordTime(DateTool.getTimestamp());
            }
            list1 = menuDao.saveAll(list);
        }
        if (list1 != null) {
            return ResultFactory.buildSuccessResult(list1);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 角色信息模糊查询
     */
    @Override
    public Result serachByInfo(String[] info) {
        List<Menus> list = null;
        if (info[0].equals("menuName")) {

            list = menuDao.findByMenuNameLike("%" + info[1] + "%");

        } else if (info[0].equals("menuPath")) {
            list = menuDao.findByMenuPathLike("%" + info[1] + "%");
        } else if (info[0].equals("menuOrder")) {
            list = menuDao.findByMenuOrderLike("%" + info[1] + "%");
        }
        System.out.println(info.toString() + "like==========" + list);

        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 根据角色编号批量删除用户信息
     */
    @Override
    public Result deleteMenuByIds(Map<String, String> menuIds) {
        List<Integer> listId = new ArrayList<Integer>();
        int row = 0;
        if (menuIds != null) {
            String ids = menuIds.get("menuIds");
            String[] id = ids.split(",");

            for (int i = 0; i < id.length; i++) {
                listId.add(Integer.parseInt(id[i]));

            }
        }
        System.out.println(listId);
        if (listId != null) {
            row = menuDao.deleteByMenuIdIn(listId);
        }

        if (row > 0) {
            return ResultFactory.buildDefaultSuccessResult();
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }
}
